﻿using System.Windows.Media;

namespace Gmantis.Imaging
{
    internal abstract class ImageResizeFilter
    {
        protected ImageResizeFilter()
        {
        }

        protected abstract double GetRadius();
        protected abstract double GetWeight(double x);
        public void Resize(Bitmap src, Bitmap dst)
        {
            double num = ((double)dst.Width) / ((double)src.Width);
            double num2 = this.GetRadius() / num;
            double num3 = ((double)dst.Height) / ((double)src.Height);
            double num4 = this.GetRadius() / num3;
            for (int i = 0; i < dst.Width; i++)
            {
                for (int j = 0; j < dst.Height; j++)
                {
                    double num7 = (i + 0.5) / num;
                    double num8 = (j + 0.5) / num3;
                    double num9 = 0.0;
                    double num10 = 0.0;
                    double num11 = 0.0;
                    double num12 = 0.0;
                    double num13 = 0.0;
                    for (int k = (int)(num7 - num2); k <= ((int)(num7 + num2)); k++)
                    {
                        if ((k >= 0) && (k < src.Width))
                        {
                            double weight = this.GetWeight((num7 - k) - 0.5);
                            if (weight > 0.0)
                            {
                                for (int m = (int)(num8 - num4); m <= ((int)(num8 + num4)); m++)
                                {
                                    if ((m >= 0) && (m < src.Height))
                                    {
                                        double num17 = this.GetWeight((num8 - m) - 0.5);
                                        if (num17 > 0.0)
                                        {
                                            Color pixel = src.GetPixel(k, m);
                                            double num18 = weight * num17;
                                            num9 += num18;
                                            num10 += pixel.A * num18;
                                            num11 += pixel.R * num18;
                                            num12 += pixel.G * num18;
                                            num13 += pixel.B * num18;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    num10 /= num9;
                    num11 /= num9;
                    num12 /= num9;
                    num13 /= num9;
                    Color color2 = Color.FromArgb((byte)num10, (byte)num11, (byte)num12, (byte)num13);
                    dst.SetPixel(i, j, color2);
                }
            }
        }
    }
}
